/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.RoleAuthorizeReq;
import org.cshoo.tattoo.authorize.app.RoleAppService;
import org.cshoo.tattoo.authorize.domain.po.Role;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/role")
@Slf4j
public class RoleController {

    @Resource
    private RoleAppService roleAppService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response<?> addRole(@RequestBody Role role) {
        roleAppService.addRole(role);
        return Response.builder().build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Response<?> updateRole(@RequestBody Role role) {
        roleAppService.updateRole(role);
        return Response.builder().build();
    }

    @RequestMapping(value = "/findRolesByNameLike", method = RequestMethod.GET)
    public List<Role> findRolesByNameLike(@RequestParam("name") String name) {
        log.info("name:{}", name);
        return roleAppService.findRolesByNameLike(name);
    }

    @RequestMapping(value = "/findRolesByGroupId/{groupId}", method = RequestMethod.GET)
    public List<Role> findRolesByGroupId(@PathVariable("groupId") Long groupId) {
        log.info("groupId:{}", groupId);
        return roleAppService.findRolesByGroupId(groupId);
    }

    @RequestMapping(value = "/findRolesByUserId/{userId}")
    public List<Role> findRolesByUserId(@PathVariable("userId") Long userId) {
        log.info("userId:{}", userId);
        return roleAppService.findRolesByUserId(userId);
    }

    @RequestMapping(value = "/all/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public Page<Role> findAllRoles(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return roleAppService.findAllRoles(pageNum, pageSize);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Response<?> deleteRoles(@RequestBody List<Long> ids) {
        roleAppService.deleteRoles(ids);
        return Response.builder().build();
    }

    @RequestMapping(value = "/roleAuthorize", method = RequestMethod.POST)
    public Response<?> roleAuthorize(@RequestBody RoleAuthorizeReq roleAuthorizeRequestVO) {
        log.info("roleAuthorizeRequestVO:{}", roleAuthorizeRequestVO);
        roleAppService.authorize(roleAuthorizeRequestVO.getRoleId(), roleAuthorizeRequestVO.getAuthorityIds());
        return Response.builder().build();
    }

    @RequestMapping(value = "/cancelAuthorities", method = RequestMethod.DELETE)
    public Response<?> cancelAuthorities(@RequestBody RoleAuthorizeReq roleAuthorizeRequestVO) {
        log.info("roleAuthorizeRequestVO:{}", roleAuthorizeRequestVO);
        roleAppService.cancelAuthorities(roleAuthorizeRequestVO.getRoleId(), roleAuthorizeRequestVO.getAuthorityIds());
        return Response.builder().build();
    }

    @RequestMapping(value = "authorizeMenu", method = RequestMethod.POST)
    public Response<?> authorizeMenu(@RequestParam("roleId") Long roleId, @RequestParam("menuId") Long menuId) {
        log.info("roleId:{},menuId:{}", roleId, menuId);
        roleAppService.authorizeMenu(roleId, menuId);
        return Response.builder().build();
    }

    @RequestMapping(value = "cancelMenuAuthority", method = RequestMethod.DELETE)
    public Response<?> cancelMenuAuthority(@RequestParam("roleId") Long roleId, @RequestParam("menuId") Long menuId) {
        log.info("roleId:{},menuId:{}", roleId, menuId);
        roleAppService.cancelMenuAuthority(roleId, menuId);
        return Response.builder().build();
    }

    @RequestMapping(value = "authorizeButton", method = RequestMethod.POST)
    public Response<?> authorizeButton(@RequestParam("roleId") Long roleId, @RequestParam("buttonId") Long buttonId) {
        log.info("roleId:{},menuId:{}", roleId, buttonId);
        roleAppService.authorizeButton(roleId, buttonId);
        return Response.builder().build();
    }

    @RequestMapping(value = "cancelButtonAuthority", method = RequestMethod.DELETE)
    public Response<?> cancelButtonAuthority(@RequestParam("roleId") Long roleId, @RequestParam("buttonId") Long buttonId) {
        log.info("roleId:{},menuId:{}", roleId, buttonId);
        roleAppService.cancelButtonAuthority(roleId, buttonId);
        return Response.builder().build();
    }
}
